package com.catmiao.rdd.operate.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
 * @author ChengMiao
 * @title: Transfer_01_Map
 * @projectName spark_study
 * @description: TODO
 * @date 2024/11/25 16:27
 */
public class Transfer_13_groupByKey {

    public static void main(String[] args) throws InterruptedException {

        final SparkConf conf = new SparkConf();
        conf.setAppName("appName");
        conf.setMaster("local[*]");

        final JavaSparkContext jsc = new JavaSparkContext(conf);


        JavaPairRDD<String, Integer> pairRDD = jsc.parallelizePairs(
                Arrays.asList(
                        new Tuple2<String, Integer>("a", 1),
                        new Tuple2<String, Integer>("b", 2),
                        new Tuple2<String, Integer>("a", 3),
                        new Tuple2<String, Integer>("b", 4)
                )
        );

        /**
         *
         * groupBy [(a,1),(a,3)]
         * groupByKey [a,[1,3]]  按照key 对value进行分组
         */
        JavaPairRDD<String, Integer> result = pairRDD.groupByKey().mapValues(iter -> {
            int num = 0;
            for (Integer i : iter) {
                num += i;
            }
            return num;
        });
        result.collect().forEach(System.out::println);

        jsc.close();


    }
}
